<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>memcached安装 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>memcached安装</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Memcached/">Memcached</a>
			</span>
		
	</div>

	

	
		<h2 id="1-memcached安装"><a href="#1-memcached安装" class="headerlink" title="1.memcached安装"></a>1.memcached安装</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># yum install -y epel-release</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># yum install -y libevent memcached libmemcached</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动 /etc/init.d/memcached start</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># service memcached start</span></div><div class="line">正在启动 memcached： [确定]</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># ps aux |grep memcached</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/01.png?raw=true" alt="01"><figcaption class="figure__caption">01</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;memcached -d -p 11211 -u memcached -m 64 -c 1024 -P /var/run/memcached/memcached.pid</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;相关的参数在/etc/init.d/memcached和/etc/sysconfig/memcached中定义</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># vim /etc/sysconfig/memcached</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/02.png?raw=true" alt="02"><figcaption class="figure__caption">02</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/03.png?raw=true" alt="03"><figcaption class="figure__caption">03</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将内存改为 128m</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># /etc/init.d/memcached restart</span></div><div class="line">停止 memcached：     [确定]</div><div class="line">正在启动 memcached：  [确定]</div></pre></td></tr></table></figure>
<h3 id="memcached-启动参数"><a href="#memcached-启动参数" class="headerlink" title="memcached 启动参数"></a>memcached 启动参数</h3><ul>
<li>-d选项是启动一个守护进程</li>
<li>-m是分配给Memcache使用的内存数量，单位是MB，这里是200MB </li>
<li>-u是运行Memcache的用户，如果当前为 root 的话，需要使用此参数指定用户。 </li>
<li>-l是监听的服务器IP地址</li>
<li>-p是设置Memcache监听的端口，默认是11211</li>
<li>-c选项是最大运行的并发连接数，默认是1024</li>
<li>-P是设置保存Memcache的pid文件 </li>
</ul>
<h3 id="查看-memcached-运行状态"><a href="#查看-memcached-运行状态" class="headerlink" title="查看 memcached 运行状态"></a>查看 memcached 运行状态</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># memcached-tool 127.0.0.1:11211 stats</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/04.png?raw=true" alt="04"><figcaption class="figure__caption">04</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># echo stats | nc 127.0.0.1:11211</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;需要安装nc工具</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># yum install -y nc</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/05.png?raw=true" alt="05"><figcaption class="figure__caption">05</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;若安装libmemcached后，可以使用命令 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;memstat –servers=127.0.0.1:11211 查看memcached服务状态</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># memstat --servers=127.0.0.1:11211</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/06.png?raw=true" alt="06"><figcaption class="figure__caption">06</figcaption></figure></p>
<h2 id="2-memcached-基本命令"><a href="#2-memcached-基本命令" class="headerlink" title="2.memcached 基本命令"></a>2.memcached 基本命令</h2><h3 id="memcached-命令行"><a href="#memcached-命令行" class="headerlink" title="memcached 命令行"></a>memcached 命令行</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># telnet 127.0.0.1 11211</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/07.png?raw=true" alt="07"><figcaption class="figure__caption">07</figcaption></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/08.png?raw=true" alt="08"><figcaption class="figure__caption">08</figcaption></figure></p>
<h3 id="mamcached-语法规则"><a href="#mamcached-语法规则" class="headerlink" title="mamcached 语法规则"></a>mamcached 语法规则</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;<span class="built_in">command</span> name&gt; &lt;key&gt; &lt;flags&gt; &lt;exptime&gt; &lt;bytes&gt;\r\n &lt;data block&gt;\r\n</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注：\r\n在windows下是Enter键</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;a) <command name=""> 可以是”set”, “add”, “replace”。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;“set”表示按照相应的<key>存储该数据，没有的时候增加，有的覆盖。</key></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; “add”表示按照相应的<key>添加该数据,但是如果该<key>已经存在则会操作失败。</key></key></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; “replace”表示按照相应的<key>替换数据,但是如果该<key>不存在则操作失败。</key></key></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;b) <key> 客户端需要保存数据的key。</key></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;c) <flags> 是一个16位的无符号的整数(以十进制的方式表示)。</flags></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该标志将和需要存储的数据一起存储,并在客户端get数据时返回。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户可以将此标志用做特殊用途，此标志对服务器来说是不透明的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;d) <exptime> 过期的时间。</exptime></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;若为0表示存储的数据永远不过期(但可被服务器算法：LRU 等替换)。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果非0(unix时间或者距离此时的秒数),当过期后,服务器可以保证用户得不到该数据(以服务器时间为标准)。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;e) <bytes> 需要存储的字节数，当用户希望存储空数据时<bytes>可以为0</bytes></bytes></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;f) <data block="">需要存储的内容，输入完成后，最后客户端需要加上”\r\n”（直接点击Enter）作为”命令头”的结束标志。</data></p>
<h3 id="memcached-数据示例"><a href="#memcached-数据示例" class="headerlink" title="memcached 数据示例"></a>memcached 数据示例</h3><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/09.png?raw=true" alt="09"><figcaption class="figure__caption">09</figcaption></figure></p>
<h2 id="3-PHP-连接-memcached"><a href="#3-PHP-连接-memcached" class="headerlink" title="3.PHP 连接 memcached"></a>3.PHP 连接 memcached</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;先安装php的memcache扩展</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># wget http://www.lishiming.net/data/attachment/forum/memcache-2.2.3.tgz</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># tar zxf memcache-2.2.3.tgz </span></div><div class="line">[root@memcached ~]<span class="comment"># cd memcache-2.2.3</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached memcache-2.2.3]<span class="comment"># /usr/local/php/bin/phpize</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/10.png?raw=true" alt="10"><figcaption class="figure__caption">10</figcaption></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached memcache-2.2.3]<span class="comment"># ./configure --with-php-config=/usr/local/php/bin/php-config</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached memcache-2.2.3]<span class="comment"># make &amp;&amp; make install</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装完后会有类似这样的提示：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">Installing shared extensions: /usr/<span class="built_in">local</span>/php/lib/php/extensions/no-debug-non-zts-20131226/</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/11.png?raw=true" alt="11"><figcaption class="figure__caption">11</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;把这个记住，然后修改php.ini</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached memcache-2.2.3]<span class="comment"># vim /usr/local/php/etc/php.ini</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;把 extension_dir = “./“ 修改为<br>extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/“</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/12.png?raw=true" alt="12"><figcaption class="figure__caption">12</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/13.png?raw=true" alt="13"><figcaption class="figure__caption">13</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;并添加一行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">extension = <span class="string">"memcache.so"</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/14.png?raw=true" alt="14"><figcaption class="figure__caption">14</figcaption></figure></p>
<h3 id="PHP-连接-memcached-检验"><a href="#PHP-连接-memcached-检验" class="headerlink" title="PHP 连接 memcached 检验"></a>PHP 连接 memcached 检验</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;检查模块是否添加正常</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached memcache-2.2.3]<span class="comment"># /usr/local/php/bin/php -m | grep memcache</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/15.png?raw=true" alt="15"><figcaption class="figure__caption">15</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;模块中有 memcache 说明配置成功</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载测试php脚本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached memcache-2.2.3]<span class="comment"># curl www.apelearn.com/study_v2/.memcache.txt &gt; 1.php 2&gt;/dev/null</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行脚本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached memcache-2.2.3]<span class="comment"># /usr/local/php/bin/php 1.php</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/16.png?raw=true" alt="16"><figcaption class="figure__caption">16</figcaption></figure></p>
<h2 id="4-memcached-实现-session-共享"><a href="#4-memcached-实现-session-共享" class="headerlink" title="4.memcached 实现 session 共享"></a>4.memcached 实现 session 共享</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;本实例是在lamp/lnmp环境下实现</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑php.ini添加两行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># vim /usr/local/php/etc/php.ini</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">session.save_handler = memcache </div><div class="line">session.save_path = <span class="string">"tcp://127.0.0.1:11211"</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/17.png?raw=true" alt="17"><figcaption class="figure__caption">17</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果 session 服务器在远程这里输入远程的 IP 地址</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者httpd.conf中对应的虚拟主机中添加</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># vim /usr/local/apache2/conf/httpd.conf</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">php_value session.save_handler <span class="string">"memcache"</span> </div><div class="line">php_value session.save_path <span class="string">"tcp://192.168.0.9:11211"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 或者php-fpm.conf对应的pool中添加</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">php_value[session.save_handler] = memcache</div><div class="line">php_value[session.save_path] = <span class="string">" tcp://192.168.0.9:11211 "</span></div></pre></td></tr></table></figure>
<h3 id="memcached-session测试"><a href="#memcached-session测试" class="headerlink" title="memcached session测试"></a>memcached session测试</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># wget http://study.lishiming.net/.mem_se.txt</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># mv .mem_se.txt /usr/local/apache2/htdocs/session.php</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># curl localhost/session.php</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/18.png?raw=true" alt="18"><figcaption class="figure__caption">18</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;类似于1443702394<br><br>1443702394<br><br>i44nunao0g3o7vf2su0hnc5440</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># telnet 127.0.0.1 11211</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/19.png?raw=true" alt="19"><figcaption class="figure__caption">19</figcaption></figure></p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Memcached/2. memcached安装/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
